/**
 * @param {number[]} prices
 * @return {number}
 */
var maxProfit = function(prices) {
  let salePrice = 0, buyPrice = -prices[0];
  for(let i = 1; i < prices.length; i++) {
    salePrice = Math.max(salePrice, buyPrice + prices[i]);
    buyPrice = Math.max(buyPrice, -prices[i]);
  }
  return salePrice;
};

let prices = [7,1,5,3,6,4];
console.log(maxProfit(prices));
prices = [7,6,4,3,1];
console.log(maxProfit(prices));
